package {{.Models}}

{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
	{{range .Imports}}"{{.}}"{{end}}
	"log"
)
{{end}}
{{$i := 0}}
{{range .Tables}}
 // {{ .Comment}}
type {{Mapper .Name}} struct {
{{$table := .}}
{{range .ColumnsSeq}}{{$col := $table.GetColumn .}}	{{Mapper $col.Name}}	{{Type $col}} {{Tag $table $col}} // {{ $col.Comment}}
{{end}}
}

func Get{{Mapper .Name}}List4Pager(form SearchForm) *PageResult {
	sqlWhere := " 1=1 " // todo sql条件 可以自定义

	params := make([]interface{}, 0)

    // todo sql条件 可以自定义
    // i := 1
	// key := form.GetStr("key")
	// if len(key) > 0 {
	// 	params = append(params, "%"+key+"%")
	// 	sqlWhere += " and (key like $" + utils.IntToStr(i) + " or value like $" + utils.IntToStr(i) +
	// 		" or remark like $" + utils.IntToStr(i) + " or name like $" + utils.IntToStr(i) + ")"
	// 	i++
	// }
	return PageHavePre("", "", "*", "{{.Name}}", sqlWhere, "sort", form.Page, params...)
}


// 删除
func Delete{{Mapper .Name}}(id int64) int64 {
	sqlSave := "delete from {{.Name}}  where id=:id"
	db, _ := OpenDB()
	defer db.Close()
	param := map[string]interface{}{"id": id}
	result, err := db.NamedExec(sqlSave, param)
	if err != nil {
		log.Fatalln(err)
		return 0
	} else {
		i, _ := result.RowsAffected()
		return i
	}
}


// 添加用户
func Add{{Mapper .Name}}Data(data *{{Mapper .Name}}) int64 {
    {{$i = 0}}
	sqlSave := `
		INSERT INTO {{.Name}}({{range .ColumnsSeq}} {{if $i}} {{.}}{{else}},{{.}} {{end}} {{$i = 1}}{{end}})
		{{$i = 0}}
		VALUES ({{range .ColumnsSeq}} {{if $i}} {{.}}{{else}},{{.}} {{end}} {{$i = 1}}{{end}})`
	db, _ := OpenDB()
	defer db.Close()
	result, err := db.NamedExec(sqlSave, data)
	if err != nil {
		log.Fatalln(err)
		return 0
	} else {
		i, _ := result.RowsAffected()
		return i
	}
}

// 更新
func Update{{Mapper .Name}}(data *{{Mapper .Name}}) int64 {
	sqlSave := `update {{.Name}} set
	{{$i = 0}}
	{{range .ColumnsSeq}} {{if $i}} {{else}},{{end}} {{.}}=:{{.}} {{$i = 1}}{{end}}
	where id=:id`
	db, _ := OpenDB()
	defer db.Close()
	result, err := db.NamedExec(sqlSave, data)
	if err != nil {
		log.Fatalln(err)
		return 0
	} else {
		i, _ := result.RowsAffected()
		return i
	}
}
{{end}}